/**
 * This example shows how to post status to Twitter.
 */

package net.widevision.android;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.widevision.ListActivity.UserProfile;

public class TestPost extends Activity {
	private TwitterApp mTwitter;
	private String screenName;
	Button postBtn ;
	EditText reviewEdit;
	TextView charCount;
//	private static final String twitter_consumer_key = "JXmXcE5yFPLIRCCtQ79eg";
//	private static final String twitter_secret_key = "dnVojAwaewvEt8E2QwLYtavwm8cdUHwph7lZCaQPMY";
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.post);
		
		postBtn  = (Button) findViewById(R.id.submit);
		reviewEdit = (EditText) findViewById(R.id.revieew);
		charCount  =(TextView) findViewById(R.id.charCount);
		charCount  =(TextView) findViewById(R.id.charCount);
		
		
        Bundle bundle=getIntent().getExtras();
		
        screenName=bundle.getString("SCREENNAME");
		
		InputFilter[] FilterArray = new InputFilter[1];
		FilterArray[0] = new InputFilter.LengthFilter(140);
		reviewEdit.setFilters(FilterArray);

		reviewEdit.addTextChangedListener(new TextWatcher(){
            public void afterTextChanged(Editable s) {
            	charCount.setText((140-s.length())+"");
            	charCount.setTextSize(10);
             }
             public void beforeTextChanged(CharSequence s, int start, int count, int after){
             	
             }
             public void onTextChanged(CharSequence s, int start, int before, int count){
             	
             }
         }); 
		
		postBtn.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String review = reviewEdit.getText().toString();
				
				if (review.equals("")) 
				return;
				postToTwitter(review);
				startActivity(intent);
				finish();
			}
			
		});
        reviewEdit.setText("@"+screenName+" ");
		
	}
		   Intent intent=new Intent(TestPost.this,UserProfile.class);

		   private void postToTwitter(final String review) {
		new Thread() {
			@Override
			public void run() {
				int what = 0;
				
				try {
					mTwitter.updateStatus(review);
				} catch (Exception e) {
					what = 1;
				}
				
				mHandler.sendMessage(mHandler.obtainMessage(what));
			}
		}.start();
	}
	
	private Handler mHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			String text = (msg.what == 0) ? "Posted to Twitter" : "Post to Twitter failed";
			Log.d("Posted tweet>>>>>>>>>>>>", text);
			
		}
	};
}